Here is my test program if you want to examine it. It's not very good and depending on your CPU/OS factorial 21 might be too big for unsigned long long.
Code:
#include <stdio.h>
double estimate_e(unsigned terms);
int main(void)
{
const int accuracy = 21; /* "degree" of "accuracy" */
double e;
e = estimate_e(accuracy);
printf("e is %.17f\n", e);
return 0;
}
double estimate_e(unsigned terms)
{
unsigned i = 0;
unsigned long long fact = 1; /* current i factorial */
double e = 0; /* current estimated value of e */
while (i <= terms) {
double prev_e = e;
if (i > 1) {
fact *= i;
/* should check for overflow of 'fact' here */
}
e += 1.0 / fact;
if (e == prev_e) /* it's bad to use == with floating point operands, but here I chose to use it */
printf("for n == %u e is identical to the previous value of e\n", i);
i++;
}
return e;
}
On my computer this gives e accurate to 14 decimal places.